package com.itheima.reggie.controller;

import com.itheima.reggie.common.PageBean;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 套餐管理控制层
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private HttpSession session;

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @RequestMapping("/page")
    public R<PageBean> findPage(int page,int pageSize,String name){
        return setmealService.findPage(page,pageSize,name);
    }

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto){
        long loginUser= (long) session.getAttribute("employee");
        //设置操作的用户
        setmealDto.setCreateUser(loginUser);
        setmealDto.setUpdateUser(loginUser);
        return setmealService.addSetMeal(setmealDto);
    }

    /**
     * 根据id查询套餐以及套餐内菜品数据回显前端
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> findSetmealDtoById(@PathVariable("id") Long id){
        return setmealService.findSetmealDtoById(id);
    }


    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
        long loginUser= (long) session.getAttribute("employee");
        //设置操作的用户
        setmealDto.setUpdateUser(loginUser);
        return setmealService.updateSetmeal(setmealDto);
    }

    /**
     * 批量修改套餐售卖状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateSetmealStatusByIds(@PathVariable("status") int status,long[] ids){
       return setmealService.updateSetmealStatusByIds(status,ids);
    }

    @DeleteMapping
    public R<String> deleteSetmealByIds(Long[] ids){
        return setmealService.deleteSetmealByIds(ids);
    }

    /**
     * 查询套餐列表
     * @param
     * @return
     */
    @RequestMapping("/list")
    public R<List<SetmealDto>> findsetmealListById(Long categoryId, Integer status){
        List<SetmealDto> setmealDtoList = setmealService.findsetmealListById(categoryId, status);
        return setmealDtoList!=null?R.success(setmealDtoList):R.error("查询失败");
    }

    @GetMapping("dish/{id}")
    public R<List<SetmealDishDto>> findsetmealDishById(@PathVariable("id") Long setmealId){
        return setmealService.findsetmealDishById(setmealId);
    }

}
